﻿using ShoppingOnline.App_Code;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace ShoppingOnline
{
    public partial class Product : System.Web.UI.Page
    {
        // 区名
        public static string quming = "装饰摆饰";
        // 查询关键字
        public static string query = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["item"] != null)
                {
                    // 获取查询字符串中的item参数作为区名
                    quming = Request.QueryString["item"].ToString();
                    string strsql;
                    if (!quming.Equals("全部"))
                    {
                        // 构建SQL语句，根据区名获取对应的商品信息
                        strsql = "select * from tb_goods where sortid =(select sortid from tb_sort WHERE sortname='" + quming + "') order by goodsid desc";
                    }
                    else
                    {
                        // 构建SQL语句，获取全部商品信息
                        strsql = "select * from tb_goods order by goodsid desc";
                    }

                    DataTable dt = new DataTable();
                    dt = SqlHelper.ExecDataSet(strsql).Tables[0];
                    GetRptBind(dt);
                }
                else if (Request.QueryString["query"] != null)
                {
                    // 获取查询字符串中的query参数作为查询关键字
                    query = Request.QueryString["query"].ToString();
                    quming = "全部";
                    string strquery = "select * from tb_goods where goodsname like  '%" + query + "%' order by goodsid desc";
                    DataTable dt = new DataTable();
                    dt = SqlHelper.ExecDataSet(strquery).Tables[0];
                    GetRptBind(dt);
                }
                else
                {
                    // 重定向到首页
                    Response.Redirect("Index.aspx");
                    return;
                }
            }
        }

        // 数据绑定
        private void GetRptBind(DataTable dt)
        {
            rpt.DataSource = dt;
            rpt.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            string strsql;
            DataTable dt = new DataTable();
            if (!quming.Equals("全部"))
            {
                // 构建SQL语句，根据区名获取对应的商品信息
                strsql = "select * from tb_goods where pType = '" + quming + "' order by goodsid desc";
                dt = SqlHelper.ExecDataSet(strsql).Tables[0];
            }
            else if (query == "")
            {
                // 构建SQL语句，获取全部商品信息
                strsql = "select * from tb_goods order by goodsid desc";
                dt = SqlHelper.ExecDataSet(strsql).Tables[0];
            }
            else
            {
                // 构建SQL语句，根据标题匹配查询关键字的商品信息
                strsql = "select * from tb_goods where pTitle like @title order by goodsid desc";
                SqlParameter para = new SqlParameter("@title", query);
                dt = SqlHelper.ExecDataSet(strsql, para).Tables[0];
            }

            GetRptBind(dt);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            // 将用户输入的搜索关键字作为查询字符串参数，重定向到产品页面进行搜索
            Response.Redirect("Product.aspx?query=" + txtQuery.Text);
        }

    }
}
